package com.mq.dpca.file;

import java.io.File;
import java.io.FileInputStream;

import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
import com.mq.dpca.msg.MQConfig;
import com.mq.dpca.util.Ping;
import com.mq.dpca.util.RenameUtil;
import com.mq.dpca.util.UuidUtil;

/**
 * 
 * MQ���鷢���ļ�����
 * 
 */
/**
 * @author Administrator
 * 
 */
public class MQFileSender {

	final int BUFFER_LEN = 1024 * 1024 * 3; // ���巢���ļ��Ĵ�С,4��

	private MQQueueManager qmgr; // ���ӵ����й�����

	private MQQueue outQueue; // �������

	private String queueName = "Q1"; // �������

	private String host = "127.0.0.1"; // �������

	private int port = 1414; // �������Ķ˿ں�

	private String channel = null;; // ͨ�����

	public static String qmgrName = null; // ���й�����

	private MQMessage outMsg; // ������Ϣ����

	private MQPutMessageOptions pmo; // ���û�ȡ��Ϣѡ��

	private static String fileName; // Ҫ������Ϸ����ļ�

	private int ccsid = 0;

	private static String file_dir = null;

	public static String fname = null;

	private static String key = null;
	
	public static String sourceApp = null;

	public static String getFile_dir() {
		return file_dir;
	}

	public static void setFile_dir(String file_dir) {
		MQFileSender.file_dir = file_dir;
	}
	
	

	public static String getKey() {
		return key;
	}

	public static void setKey(String key) {
		MQFileSender.key = key;
	}

	public static String getSourceApp() {
		return sourceApp;
	}

	public static void setSourceApp(String sourceApp) {
		MQFileSender.sourceApp = sourceApp;
	}

	// 104857000
	//
	public void runGoupSender() {
		try {
			init();
			sendGroupMessages();
			qmgr.commit();
			System.out.println("\n Messages successfully Send ");
		} catch (MQException mqe) {
			mqe.printStackTrace();
			try {
				System.out.println("\n Backing out Transaction ");
				qmgr.backout();
				System.exit(2);
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(2);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(2);
		}
	}

	public void initproperty(String key) {
		MQConfig config = new MQConfig().getNewDbConfigFromKey(key);
		if (config.getMQ_MANAGER() != null) {
			qmgrName = config.getMQ_MANAGER();
			queueName = config.getMQ_QUEUE_NAME();
			channel = config.getMQ_CHANNEL();
			host = config.getMQ_HOST_NAME();
			port = Integer.valueOf(config.getMQ_PROT());
			ccsid = Integer.valueOf(config.getMQ_CCSID());
			file_dir = config.getFILE_DIR();
		}
	}

	/**
	 * ��ʼ��������������Ϣ
	 * 
	 * @throws Exception
	 */
	private void init() throws Exception {
		/** ����MQEnvironment �����Ա�ͻ������� */
		String hosts[] = host.split(";");
		String qmNames[] = qmgrName.split(";");
		String gt_ip = "";
		String gt_qmn = "";
		if (Ping.isAddressAvailable(hosts[0])) {
			gt_ip = hosts[0];
			gt_qmn = qmNames[0];
		} else {
			gt_ip = hosts[1];
			gt_qmn = qmNames[1];
		}
		MQEnvironment.hostname = gt_ip; 
		
		MQEnvironment.channel = channel;
		MQEnvironment.port = port;
		MQEnvironment.CCSID = ccsid;
		// MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE
		// MQ);
		/* ���ӵ����й����� */
		qmgr = new MQQueueManager(gt_qmn);
		/* ���ö��д�ѡ���Ա���� */
		int opnOptn = MQConstants.MQOO_OUTPUT
				| MQConstants.MQOO_FAIL_IF_QUIESCING;
		// int opnOptn = MQConstants.MQOO_OUTPUT ;
		outQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
	}

	/**
	 * ���͵�������
	 * 
	 * @throws Exception
	 */
	public void sendGroupMessages() throws Exception {
		/* ���÷�����Ϣѡ�� */
		String uid = UuidUtil.getUUID();
		pmo = new MQPutMessageOptions();
		pmo.options = pmo.options + MQConstants.MQPMO_LOGICAL_ORDER;
		pmo.options = pmo.options + MQConstants.MQPMRF_GROUP_ID;
		outMsg = new MQMessage();
		/* ������Ϣ��ǣ���ʾ����Ϣ������ */
		outMsg.messageFlags = MQConstants.MQMF_MSG_IN_GROUP;
		File file = new File(fileName);
		FileInputStream fis = new FileInputStream(file);
		int leght = 0;
		while (true) {
			byte[] buffer = null;
			leght = fis.available();
			if (leght == 0) {
				break;
			}
			if (leght > BUFFER_LEN) {
				// ������Ƭ��С
				buffer = new byte[BUFFER_LEN];
				fis.read(buffer, 0, BUFFER_LEN);
			} else {
				// ����С����Ƭ��С�����ý�β��־��ţ���ʣ���ļ����ȫ�����ͣ�
				buffer = new byte[leght];
				fis.read(buffer, 0, leght);
				outMsg.messageFlags = MQConstants.MQMF_LAST_MSG_IN_GROUP;
			}
			// �����ļ����
			outMsg.setStringProperty("ESBHeader", "{\"RequestID\":\""+uid+"\",\"SourceSystem\":\""+sourceApp+"\",\"TargetSystem\":\"ESB\",\"ServiceName\":\""+key+"\",\"ServiceVersion\":\"1.0\"}");
			outMsg.setObjectProperty("fileName", fname);
//			outMsg.setObjectProperty("serviceName", key);
//			outMsg.setObjectProperty("requestId", uid);
//			outMsg.setObjectProperty("requestSystem",
//					RenameUtil.getSysForKey(key));
			outMsg.write(buffer);
			outQueue.put(outMsg, pmo);
			outMsg.clearMessage();
		}
		fis.close();
	}

	/**
	 * ��������
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
//		if (args == null || args.length < 2) {
//			System.out.println("Not specified file or interface encoding");
//		} else {
			MQFileSender mfs = new MQFileSender();
//			key = args[1];
//			mfs.initproperty(key);
//			fname = args[0];
			 key = "TO_VVN";
			 fname = "jdk1.6.0_43.rar";
			 mfs.initproperty(key);
			fileName = file_dir + fname;
			if (qmgrName == null || file_dir == null) {
				System.out
						.println("could not find the configuration by the interface encoding");
			} else {
				int i = 0;
				while (true) {
					i++;
					// �����ļ�
					mfs.runGoupSender();
					if (i == 1) {
						break;
					}
				}
			}
		}
//	}
	
	public void sendFile(String serviceName,String sourceSys,String file){
		sourceApp = sourceSys;
		key = serviceName;
		initproperty(key);
		fileName = file;
		fname = RenameUtil.getNameByUrl(file);
		runGoupSender();
	}
}